package objects; // the package needs to be exactly the same as the one of the tested file
import communication.*;
import objects.*;
import objects.distribution.*;
import kernel.*;
import comRadiusController.*;
import sleeping.*;
import java.util.*; // make all necessaries imports here
import java.awt.*;


import org.junit.*;
import static org.junit.Assert.*; // These two lines are for the JUnit

/**
 * @author  csamson
 * @version 04/2007
 * This file is for the Sensor Network Tests, in the CSMA-CA part
 * Junit tutorial
 */

 
public class SensorNetworkTest { // please name the class and the file after the name of the tested one, with Test at the end


//You have to declare here every object you will use in the tests
    private Simulator simulator;
    private SensorNetwork sensorNet;
    private DataSensorNetwork dataSensorNet;
    private DataSensor dataSensor;
    private SleepingAlgo sleepingAlgo;
    private ComRadiusController controller;
    private RandomDistribAlgo randDistribAlgo;
    private GridDistribAlgo gridDistribAlgo;
    private SnailDistribAlgo snailDistribAlgo;


@Before // this tag means that the method below will be called before each test
    public void setUp() { // setUp method creates all objects you want to use
    //General informations
    simulator = new Simulator("pouet");
    /*String netName = "test";
      float measRad = (float)7.1;
      int sensAmount = 40;
      int kCov = 3;
      int loop = 10;
      double x = 5;
      double y = 5;
      double w = 50;
      double h = 20;
      float fmin = 0;
      float fmax = 0;
      int nbfreq = 0;
      double maxReachRadius = 100;
      double carrierRate = 2.0;
      double batteryLevel = 100;
      double[] consumption = {0, 0, 0, 0};
      sensorNet = new SensorNetwork();
      controller = new VoronoiController();
      drowAlgo = new DrowsinessAlgo(sensorNet);
      distribAlgo = new RandomDistribAlgo(x,y,w,h,sensAmount);
      dataSensorNet = new DataSensorNetwork(netName, x, y, w, h,sensAmount, measRad, 
      maxReachRadius, carrierRate, batteryLevel,
      consumption, distribAlgo, new Vector(), 
      fmin, fmax, nbfreq,
      drowAlgo, controller, kCov);
      
      sensorNet.setProperties(dataSensorNet);*/
  
}
    
    
    // and now, Ladies and Gentlemen, the Tests !!!
    
    /*case 1 :  Initial KCover Test
      Normally, due to the way we initialize the SensorNetwork, we should have a 100% kCover
    */
    
    @Test // necessary before a test, you can/should/have to have numerous tests in your file
	public void testKCover()  {
	String netName = "test";
	String sensName = "sensorTest";
	double measRad = 7.1;
	int sensAmount = 60;
	int kCov = 3;
	int loop = 10;
	double x = 0;
	double y = 0;
	double w = 40;
	double h = 40;
	float fmin = 0;
	float fmax = 0;
	int nbfreq = 0;
	double maxReachRadius = 100;
	double carrierRate = 2.0;
	double batteryLevel = 100;
	double[] consumption = {0, 0, 0, 0};
	sensorNet = new SensorNetwork();
	controller = new VoronoiController();
	sleepingAlgo = new DrowsinessAlgo();
	sleepingAlgo.setSensorNetwork(sensorNet);
	randDistribAlgo = new RandomDistribAlgo(x,y,w,h,sensAmount);
	dataSensor = new DataSensor(sensName,x,y,maxReachRadius,carrierRate,
			batteryLevel,consumption,controller,
			new Vector(),fmin,fmax,nbfreq,measRad);
	dataSensorNet = new DataSensorNetwork(netName, x, y, w, h,sensAmount, randDistribAlgo, new Vector(),sleepingAlgo,10, kCov,dataSensor);
	
	sensorNet.setProperties(dataSensorNet);
	double f=sensorNet.getKCoverPercentage(); 
	assertTrue(f<=1.0); // JUnit asserts that kCover percentage is 1.0.
	
    }
    
 /*   @Test // necessary before a test, you can/should/have to have numerous tests in your file
	public void testKCover2()  {
	String netName = "test";
	String sensName = "sensorTest";
	double measRad = 7.1;
	int sensAmount = 60;
	int kCov = 3;
	int loop = 10;
	double x = 0;
	double y = 0;
	double w = 40;
	double h = 40;
	float fmin = 0;
	float fmax = 0;
	int nbfreq = 0;
	double maxReachRadius = 100;
	double carrierRate = 2.0;
	double batteryLevel = 100;
	double[] consumption = {0, 0, 0, 0};
	sensorNet = new SensorNetwork();
	controller = new VoronoiController();
	sleepingAlgo = new DrowsinessAlgo(sensorNet);
	gridDistribAlgo = new GridDistribAlgo(x,y,w,h,sensAmount);	dataSensor = new DataSensor(sensName,x,y,maxReachRadius,carrierRate,
			batteryLevel,consumption,controller,
			new Vector(),fmin,fmax,nbfreq,measRad);
	dataSensorNet = new DataSensorNetwork(netName, x, y, w, h,sensAmount, randDistribAlgo, new Vector(),sleepingAlgo,10, kCov,dataSensor);
	sensorNet.setProperties(dataSensorNet);
	double f=sensorNet.getKCoverPercentage(); 
	assertTrue(f<=1.0); // JUnit asserts that kCover percentage is 1.0.
	
    }
*/

   /* @Test // necessary before a test, you can/should/have to have numerous tests in your file
	public void testKCover3()  {
	String netName = "test";
	String sensName = "sensorTest";
	double measRad = 7.1;
	int sensAmount = 60;
	int kCov = 3;
	int loop = 10;
	double x = 0;
	double y = 0;
	double w = 40;
	double h = 40;
	float fmin = 0;
	float fmax = 0;
	int nbfreq = 0;
	double maxReachRadius = 100;
	double carrierRate = 2.0;
	double batteryLevel = 100;
	double[] consumption = {0, 0, 0, 0};
	sensorNet = new SensorNetwork();
	controller = new VoronoiController();
	sleepingAlgo = new DrowsinessAlgo(sensorNet);
	snailDistribAlgo = new SnailDistribAlgo(x,y,w,h,sensAmount, (float)measRad);
	dataSensor = new DataSensor(sensName,x,y,maxReachRadius,carrierRate,
			batteryLevel,consumption,controller,
			new Vector(),fmin,fmax,nbfreq,measRad);
	dataSensorNet = new DataSensorNetwork(netName, x, y, w, h,sensAmount, randDistribAlgo, new Vector(),sleepingAlgo,10, kCov,dataSensor);
	sensorNet.setProperties(dataSensorNet);
	double f=sensorNet.getKCoverPercentage(); 
	assertTrue(f<=1.0); // JUnit asserts that kCover percentage is 1.0.
	
    }
*/

    /*case 2 :  KCover change from 3 to 4
      we change the kCover from is initial value 3 to 4
      This might normally not be used in SimMobil but is a good test to check the validty of the algorithme
    */
    
    /*@Test 
    public void testChangeKCover()  {
    sensorNet.setKCover(4);
    double f=sensorNet.getKCoverPercentage(); 
    assertTrue(f<1.1); // JUnit asserts that kCover percentage is less than1.0.
    
    }
    */

    /*case 3 :  We put in sleeping some Sensor 
     * and check the KCover of the sensorNetwork
    */
    
	@Test
	public void testSleeping(){
		System.out.println("\n\ntest de sleeping pour le SensorNetwork");
		String netName = "test";
		String sensName = "sensorTest";
		double measRad = 10;
		int sensAmount = 100;
		int kCov = 2;
		int loop = 10;
		double x = 0;
		double y = 0;
		double w = 50;
		double h = 50;
		float fmin = 0;
		float fmax = 0;
		int nbfreq = 0;
		double maxReachRadius = 100;
		double carrierRate = 2.0;
		double batteryLevel = 100;
		double[] consumption = {0, 0, 0, 0};
		sensorNet = new SensorNetwork();
		controller = new VoronoiController();
		gridDistribAlgo = new GridDistribAlgo(x,y,w,h,sensAmount);
		dataSensor = new DataSensor(sensName,x,y,maxReachRadius,carrierRate,
			batteryLevel,consumption,controller,
			new Vector(),fmin,fmax,nbfreq,measRad);
		dataSensorNet = new DataSensorNetwork(netName, x, y, w, h,sensAmount, 
						gridDistribAlgo, new Vector(),
						new NoSleepingAlgo(),5, kCov,dataSensor);
		
		sensorNet.setProperties(dataSensorNet);
		sleepingAlgo = new RandomAlgo();
		sleepingAlgo.setSensorNetwork(sensorNet);
		sensorNet.setSleepingAlgo(sleepingAlgo);
		double f=sensorNet.getKCoverPercentage();
		sensorNet.calculateSleeping();
		f=sensorNet.getKCoverPercentage();

		for(int i=0;i<20;i++){
			sensorNet.calculateSleeping();
			f=sensorNet.getKCoverPercentage();
		}
		 
		assertTrue(f<=1.0); // JUnit asserts that kCover percentage is 1.0.
	}

    
    /* time to check all the tests:
       
SensorNetwork.java, ligne : 253, getKCoverPercentage : 
cellNumber 400.0, kCovCellNumber 400.0, kCover 3; resultat:1.0

SensorNetwork.java, ligne : 253, getKCoverPercentage : 
cellNumber 400.0, kCovCellNumber 192.0, kCover 4; resultat:0.48

SensorNetwork.java, ligne : 204, deleteSensor : found true
SensorNetwork.java, ligne : 253, getKCoverPercentage : 
cellNumber 400.0, kCovCellNumber 370.0, kCover 3; resultat:0.925

Time: 0,281

OK (3 tests)
    JUnit is happy, everything seems to be working
    */
}
